package com.dynamicthreadpool.example;

import com.dynamicthreadpool.manager.ThreadPoolManager;
import com.dynamicthreadpool.dubbo.DynamicThreadPoolFactory;
import com.dynamicthreadpool.spi.URL;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 动态线程池Spring Boot应用示例
 * 展示如何使用DynamicThreadPoolExecutor并通过URL控制线程池
 */
@SpringBootApplication(scanBasePackages = {"com.dynamicthreadpool.spring"})
public class DynamicThreadPoolSpringBootApplication {

    public static void main(String[] args) {
        // 启动Spring Boot应用
        ConfigurableApplicationContext context = SpringApplication.run(DynamicThreadPoolSpringBootApplication.class, args);
        
        // 添加关闭钩子，确保应用退出时正确关闭线程池
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.out.println("正在关闭线程池...");
            ThreadPoolManager.getInstance().shutdownAll();
            System.out.println("线程池已关闭");
            context.close();
        }));
        
        // 获取ThreadPoolManager实例
        ThreadPoolManager threadPoolManager = ThreadPoolManager.getInstance();
        
        // 创建一个示例URL，用于动态创建线程池
        URL url = new URL("dynamic", "localhost", 8888)
                .setParameter("corethreads", "10")
                .setParameter("maxthreads", "50")
                .setParameter("queues", "100")
                .setParameter("keepalive", "60000")
                .setParameter("queueType", "LinkedBlockingQueue")
                .setParameter("rejectPolicy", "abort")
                .setParameter("threadname", "example-threadpool");
        
        // 使用DynamicThreadPoolFactory创建线程池
        java.util.concurrent.Executor executor = DynamicThreadPoolFactory.getExecutor(url);
        
        // 注册线程池到管理器
        threadPoolManager.registerThreadPool("exampleThreadPool", executor);
        
        System.out.println("示例线程池已创建并注册: exampleThreadPool");
        System.out.println("线程池信息: " + executor);
        
        // 提交一些任务到线程池进行测试
        System.out.println("\n提交测试任务到线程池:");
        for (int i = 0; i < 5; i++) {
            final int taskId = i;
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " 正在执行任务: " + taskId);
                try {
                    // 模拟任务执行时间
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                System.out.println(Thread.currentThread().getName() + " 完成任务: " + taskId);
            });
        }
        
        // 模拟通过URL动态更新线程池参数
        System.out.println("\n演示通过URL动态更新线程池参数:");
        URL updateUrl = new URL("dynamic", "localhost", 8888)
                .setParameter("corethreads", "20")
                .setParameter("maxthreads", "100")
                .setParameter("queues", "200")
                .setParameter("threadname", "updated-example-threadpool");
        
        // 更新线程池参数
        threadPoolManager.updateThreadPool("exampleThreadPool", updateUrl);
        
        // 再次获取线程池并打印信息，验证更新
        java.util.concurrent.Executor updatedExecutor = threadPoolManager.getThreadPool("exampleThreadPool");
        System.out.println("更新后的线程池信息: " + updatedExecutor);
        
        System.out.println("\nNetty服务器已启动，可以通过客户端连接进行线程池控制");
    }
}